<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="content"
        style="height:150px;line-height:150px;text-align:center; color: #fff;background-color:#ccc;font-size:80px;">
    </div>
    <div id="btn">
        sdsdv
    </div>
    <script>
        let num = 1;
        let content = document.getElementById('content');

        function count() {
            content.innerHTML = num++;
        };
        content.onclick = count;
        function debounce(func, wait) {
            let timer;
            return function () {
                let context = this; // 注意 this 指向
                let args = arguments; // arguments中存着e

                if (timer) clearTimeout(timer);

                timer = setTimeout(() => {
                    func.apply(this, args)
                }, wait)
            }
        }
        content.onclick = debounce(count, 1000);
    </script>
    <script>
        let btn = document.getElementById('btn');
        function submit() {
            console.log('submit');
        }
        btn.onclick = submit
        function debounce1(fn, wait) {
            let timeout
            //借助闭包
            return function () {
                let context = this;
                if (timeout) clearTimeout(timeout); //进入该分支语句，说明当前正在一个计时过程中，并且又触发了相同事件。所以要取消当前的计时，重新开始计时
                timeout = setTimeout(() => {
                    fn.apply(context, arguments);
                }, wait);
            }
        }
        
        btn.onclick=debounce1(submit, 1000);
    </script>
</body>

</html>